package b3.model
{
	import b3.config.*;
	import b3.net.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	import b3.widget.match.*;
	
	public class Match extends Model
	{
		public var index:int;
		public var ranks:Array;
		public var top3:Array;
		
		public function Match(index:int)
		{
			this.index = index;
			super();
		}
		
		private function parseRankList(data:Array):void
		{
			ranks = new Array();
			for (var i:int = 0; i < data.length; i++)
			{
				var rankArray:Array = new Array();
				for (var j:int = 0; j < data[i].length; j++)
				{
					var rankInfo:RankInfo = new RankInfo();
					rankInfo.loadbyObject(data[i][j]);
					rankArray.push(rankInfo);
				}
				ranks.push(rankArray);
			}
		}
		
		//检查比赛资格
		public function checkMatchQualification(force:Boolean = false):void
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.CHECK_MATCH_QUALIFICATION, {matchIndex:index});
			serverRequest.setHandleFunc(handleCheckMatchQualification);
			serverRequest.post(force);
		}
		
		private function handleCheckMatchQualification(data:Object):void
		{
			var me:Player = PlayerManager.getInstance().me; 
			me.stateModel.handleLoadStates(data['state']);
			var modelEvent:ModelEvent = new ModelEvent(ModelEvent.CHECK_MATCH_QUALIFICATION_COMPLETE);
			modelEvent.data = data;
			dispatchEvent(modelEvent);
		}
		
		//加载比赛排名（TOP3）
		public function loadMatchTop3(force:Boolean = false):void
		{
			var pm:PlayerManager = PlayerManager.getInstance();
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.GET_MATCH_TOP3, {matchIndex:index, openid:pm.openid, openkey:pm.openkey, pf:pm.pf});
			serverRequest.setHandleFunc(handleMatchTop3);
			serverRequest.post(force);
		}
		
		private function handleMatchTop3(data:Object):void
		{
			top3 = new Array();
			for (var ranking:String in data)
			{
				var rankInfo:RankInfo = new RankInfo();
				rankInfo.loadbyObject(data[ranking]);
				top3.push(rankInfo);
			}
			dispatchEvent(new ModelEvent(ModelEvent.REFRESH_MATCH_TOP3));
		}
		
		//加载比赛所有排名
		public function loadMatchRankList(force:Boolean = false):void
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.GET_MATCH_RANK_LIST, {matchIndex:index});
			serverRequest.setHandleFunc(handleMatchRankList);
			serverRequest.post(force);
		}
		
		private function handleMatchRankList(data:Object):void
		{
			parseRankList(data as Array);
			dispatchEvent(new ModelEvent(ModelEvent.REFRESH_MATCH_RANK_LIST));
		}
		
		//增加比赛结果
		public function addMatchResult(population:int, height:int, force:Boolean = false):void
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.ADD_MATCH_RESULT, {matchIndex:index, 
				height:height, population:population, builderName:PlayerManager.getInstance().me.name});
			serverRequest.setHandleFunc(handleAddMatchResult);
			serverRequest.post(force);
		}
		
		private function handleAddMatchResult(data:Object):void
		{
			loadMatchRankList(true);
			var hasValidRanking:Boolean = false;
			for (var i:int = 0; i < data.length; i++)
			{
				var ranking:int = parseInt(data[i]);
				if (ranking >= 0)
				{
					hasValidRanking = true;
					var matchWinBox:MatchWinBox = new MatchWinBox(this, i, ranking);
					WidgetManager.getInstance().popOutOneByOne(matchWinBox);
				}
			}
			if (!hasValidRanking)
			{
				var popMessage:PopMessage = new PopMessage("未能超越原有记录 ╮(╯▽╰)╭", 0xFF0000);
				WidgetManager.getInstance().popOut([popMessage]);
			}
			var me:Player = PlayerManager.getInstance().me;
			me.taskList.finishTask(TaskConfig.ID_ATTEND_MATCH);
		}
		
	}
}